/**
 * @description 设置可过期的缓存
 */


/**
 * 新增缓存
 * @param {string} name 缓存名称
 * @param {any} storage 缓存
 * @param {number} expired 过期时间，单位秒，-1表示永久      
 */
function setStorageSync(name, storage, expired = -1) {
	uni.setStorageSync(name, {
		expired: expired,
		time: Date.now(),
		value: storage
	})
}

/**
 * 获取缓存
 * @param {string} name 缓存名称
 * @return {any} 缓存
 */
function getStorageSync(name) {
	const storage = uni.getStorageSync(name)
	if (storage.expired !== -1 && (Date.now() - parseInt(storage.time)) > (parseInt(storage.expired) * 1000)) {
		return null;
	} else {
		return storage.value
	}
}

export default {
	setStorageSync,
	getStorageSync
}